package com.jabil.util;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;

public final class JavaMailPasswordUntil {
    private JavaMailPasswordUntil() {}

    public static void sendEmail(String[] toEmailAddress,String zhuti,String[] cs,String[] ms,String text) {

        System.setProperty("javax.net.ssl.protocols", "TLSv1.2");

        // 发件人邮箱地址
        String from = "Hua_DF_Notice@jabil.com";


        // Outlook 邮箱的 SMTP 服务器地址
        String host = "10.10.7.18";

        // 获取系统属性
        Properties properties = System.getProperties();

        // 设置 Outlook 邮箱的 SMTP 服务器
        properties.setProperty("mail.smtp.host", host);
//        properties.put("mail.smtp.auth", "false");//	是否需要用户认证
//        properties.put("mail.smtp.starttls.enale", "false");//	启用TlS加密
//        properties.put("mail.smtp.ssl.enable", false);
        properties.put("mail.smtp.port", "25");
//        properties.put("mail.smtp.ssl.enable", false);

        // 获取默认的 Session 对象
        Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                // 发件人邮箱账号和密码，需要替换成你自己的账号和密码
                return new PasswordAuthentication("Hua_DF_Notice@jabil.com", "dfsllsd@9fs");
            }
        });

        try {
            // 创建一个默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);

            // 设置 From: 头部头字段
            message.setFrom(new InternetAddress(from));

            // 设置 To: 头部头字段
//            message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmailAddress));
            if(null != toEmailAddress){
                for (String email : toEmailAddress) {
                    message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
                }

            }

            // 设置 Subject: 头部头字段
            message.setSubject(zhuti);
            if(null != cs){
                for (String email : cs) {
                    message.addRecipient(Message.RecipientType.CC, new InternetAddress(email));
                }

            }

            if(null != ms){
                for (String email : ms) {
                    message.addRecipient(Message.RecipientType.BCC, new InternetAddress(email));
                }

            }

            Multipart multipart = new MimeMultipart();
            MimeBodyPart mimeBodyPart = new MimeBodyPart();
            mimeBodyPart.setContent(text,"text/html");
            multipart.addBodyPart(mimeBodyPart);

            // 设置实际消息
            message.setContent(multipart);

            // 发送消息
            Transport.send(message);
//            System.out.println("邮件发送成功。");

        } catch (MessagingException mex) {
            mex.printStackTrace();
        }

    }
}